package Demo_0429;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:
 * @DateTime:2022/4/29 10:29
 **/
public class SqStringClass {
    final int maxSize = 100;
    private char [] data;
    private int size;

    public SqStringClass() {
        data = new char[maxSize];
        size = 0;
    }

    //根据字符串 str 创建一个串
    public void strAssign(String str){
        char[] chars = str.toCharArray();
        if (chars.length > maxSize){
            throw new IllegalArgumentException("容量不够，无法创建");
        }
        for (int i = 0; i < chars.length; i++) {
            data[i] = chars[i];
        }
        size = chars.length;
    }

    //获取下标为i 的字符
    public char get(int i){
        if (i<0 || i>size){
            throw new IllegalArgumentException("参数不合理");
        }
        return data[i];
    }

    //将下标i 的值设置为 c
    public void set(int i,char c){
        if (i<0 || i>size){
            throw new IllegalArgumentException("参数不合理");
        }
        data[i] = c;
    }

    //返回由当前串，复制的一个串
    public String strCopy(){
        String str = "";
        for (char c : data) {
            str += c;
        }
        return str;
    }

    //返回串长，串中字符的个数
    public int size(){
        return size;
    }

    //返回当前串和串 t 的结果，将两个串拼接到一起
    public String concat(String t){
        char[] chars = t.toCharArray();
        if (chars.length + size > maxSize){
            throw new IllegalArgumentException("容量不够，无法添加");
        }
        for (int i = 0; i < chars.length; i++) {
            data[size+i] = chars[i];
        }
        size = chars.length+size;
        return strCopy();
    }

    //截取从下标 i 开始的 j个字符
    public String subStr(int i,int j){
        if (i<0 || i>size || j<0 || i+j >size){
            throw new IllegalArgumentException("参数不合理");
        }
        String str = "";
        for(int z = i;z<=i+j;z++){
            str += data[z];
        }
        return str;
    }

    //将串 t插入道下标i的位置
    public String insStr(int i,String t){
        char[] chars = t.toCharArray();
        if (size+chars.length > maxSize){
            throw new IllegalArgumentException("容量不够，无法插入");
        }
        for(int x = size+chars.length-1;x>i+chars.length-1;x--){
            data[x] = data[x-chars.length];
        }
        for (int z = 0; z < chars.length; z++) {
            data[i+z] = chars[z];
        }
        return strCopy();
    }

}

